// Copyright 2012 The golab Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"flag"
	"log"
	"net/http"

	"./exp"
	"./machine"
)

var (
	host       = flag.String("h", "", "address to listen on")
	configFile = flag.String("c", "", "configuration to run")
)

func serve() {
	ms := &machine.Server{
		Host: *host,
	}
	l, err := ms.Listen()
	if err != nil {
		log.Fatal("main:ms.Listen:", err)
	}
	http.Serve(l, nil)
}

func main() {
	flag.Parse()

	if *configFile == "" {
		serve()
		return
	}

	e, err := exp.NewExp(*configFile)
	if err != nil {
		log.Fatalf("main:exp.NewExp: %v", err)
	}
	err = e.Run()
	if err != nil {
		log.Fatalf("main:e.Run: %v", err)
	}
	err = e.Save()
	if err != nil {
		log.Fatalf("main:e.Save: %v", err)
	}
	err = e.Close()
	if err != nil {
		log.Fatalf("main:e.Close: %v", err)
	}
}
